package ${Configuration.packageProject}.${Configuration.packageController};

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import ${Configuration.packageProject}.annotation.Jurisdiction;
import ${Configuration.packageProject}.${Configuration.packageEntity}.${Configuration.entityClass};
import ${Configuration.packageProject}.${Configuration.packageEntity}.PageEntity;
import ${Configuration.packageProject}.${Configuration.packageInterface}.${Configuration.interfaceClass};
import ${Configuration.packageProject}.tools.Letter;
import ${Configuration.packageProject}.tools.DataTool;
import ${Configuration.packageProject}.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/${Configuration.entityName!""}Operation")
@Jurisdiction(value = "/${Configuration.entityName!""}Operation")
public class ${Configuration.entityClass}Con {
	@Autowired
	private ${Configuration.interfaceClass} ${Configuration.interfaceName} = null;

	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<${Configuration.entityClass}> getPage(@RequestBody JSONObject object){
		Letter<${Configuration.entityClass}> letter = new Letter<${Configuration.entityClass}>();
		try {
			PageEntity<${Configuration.entityClass}> result = ${Configuration.interfaceName}.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

     /**
	 * 根绝条件获取数据
	 * @return
	 */
    @Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<${Configuration.entityClass}> selectList(@RequestBody ${Configuration.entityClass} ${Configuration.entityName}){
		Letter<${Configuration.entityClass}> letter = new Letter<${Configuration.entityClass}>();
		try {

			List<${Configuration.entityClass}> result = ${Configuration.interfaceName}.selectList(${Configuration.entityName});
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个
	 * @param ${Configuration.entityName}
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<${Configuration.entityClass}> selectOne(@RequestBody ${Configuration.entityClass} ${Configuration.entityName}){
		Letter<${Configuration.entityClass}> letter = new Letter<${Configuration.entityClass}>();
		try {
			${Configuration.entityClass} obj = ${Configuration.interfaceName}.selectOne(${Configuration.entityName});
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param ${Configuration.entityName}
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody ${Configuration.entityClass} ${Configuration.entityName}){
		Letter<String> letter = new Letter<String>();
		${Configuration.entityName}.setId(DataTool.createUUID());
		Map<String , String> sub = SubjectNotNull.subject(${Configuration.entityName});
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			${Configuration.interfaceName}.insert(${Configuration.entityName});
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 * @param ${Configuration.entityName}s
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<${Configuration.entityClass}> ${Configuration.entityName}s){
		Letter<String> letter = new Letter<String>();
		try {
			${Configuration.interfaceName}.insertBatch(${Configuration.entityName}s,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 * @param ${Configuration.entityName}
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody ${Configuration.entityClass} ${Configuration.entityName}){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(${Configuration.entityName})) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			${Configuration.interfaceName}.delete(${Configuration.entityName});
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
    /**
    * 根据id批量删除
    */
    @Jurisdiction(value = "deletes")
    @RequestMapping(value = "/deletes",produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> deletes(@RequestBody List<${Configuration.entityClass}> ${Configuration.entityName}s){
        Letter<String> letter = new Letter<String>();
        if(DataTool.isBlank(${Configuration.entityName}s)) {
            letter.setResult("FAIL");
            letter.setDetail("信息不能为空！");
            return letter;
        }
        try {
			${Configuration.interfaceName}.deletes(${Configuration.entityName}s);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        }catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除失败！");
        }
        return letter;
    }

	/**
	 * 根绝id更新
	 * @param ${Configuration.entityName}
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody ${Configuration.entityClass} ${Configuration.entityName}){
		Letter<String> letter = new Letter<String>();
		if(${Configuration.entityName}.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			${Configuration.interfaceName}.updateById(${Configuration.entityName});
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;
	}
}
